import { useState, useEffect } from "react";
export function useMouse(show = false) {
  var elem = null;
  // 定义X-Y轴数据
  const [position, setPosition] = useState({ x: 0, y: 0 });

  // 鼠标移动是处理的函数
  var MouseMove = e => {
    // 更新并获取坐标
    setPosition({ x: e.clientX, y: e.clientY });
    if (show) {
      elem.style.position = "absolute";
      elem.style.left = e.clientX + "px";
      elem.style.top = e.clientY + "px";
      elem.innerHTML = `X:${e.clientX},Y:${e.clientY}`;
      elem.style.transform = "scale(0.5)";
    }
  };

  useEffect(() => {
    if (show) {
      elem = document.createElement("div");
      document.body.appendChild(elem);
    } else {
    }
    // 添加鼠标移动时间
    window.addEventListener("mousemove", MouseMove);
    // 返回 卸载时间
    return () => {
      // 移除鼠标移动时间
      window.removeEventListener("mousemove", MouseMove);
    };
  }, []);
  // 返回坐标数据
  return position;
}
